package com.zzz.record;

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.List;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;

/**
 * Hello world!
 *
 */
public class App {
	static int cc = 0;

	public static void main(String[] args) throws AWTException {
		File file = new File("E:\\aa.txt");
		List<String> readUtf8Lines = FileUtil.readUtf8Lines(file);
		String first = CollUtil.getFirst(readUtf8Lines);
		first = StrUtil.trim(first);
		List<String> split = StrUtil.split(first, "-");
		int xx1 = 0;
		int yy1 = 0;
		int xx2 = 0;
		int yy2 = 0;
		int xx3 = 0;
		int yy3 = 0;
		for (int i = 0; i < split.size(); i++) {
			String string = split.get(i);
			string = StrUtil.trim(string);
			String unWrap = StrUtil.unWrap(string, "(", ")");
			unWrap = StrUtil.trim(unWrap);
			String before = StrUtil.subBefore(unWrap, ",", false);
			String after = StrUtil.subAfter(unWrap, ",", false);
			before = StrUtil.trim(before);
			after = StrUtil.trim(after);
			if (i == 0) {
				xx1 = Integer.parseInt(before);
				yy1 = Integer.parseInt(after);
			} else if (i == 1) {
				xx2 = Integer.parseInt(before);
				yy2 = Integer.parseInt(after);
			} else if (i == 2) {
				xx3 = Integer.parseInt(before);
				yy3 = Integer.parseInt(after);
			}
		}
		try {
			Thread.sleep(7000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		Robot robot = new Robot();
		sleep(robot, 1000);
		move(robot, xx1, yy1);
		sleep(robot, 100);
		dbclick(robot);
		sleep(robot, 1000);
		click(robot);
		sleep(robot, 100);
		robot.keyPress(KeyEvent.VK_F7);
		robot.keyRelease(KeyEvent.VK_F7);
		sleep(robot, 3000);
		move(robot, 250, 419);
		sleep(robot, 1000);
		move(robot, 250, 419, xx2, yy2, 800, 100);// 移入
		sleep(robot, 500);
		dbclick(robot);
		sleep(robot, 1000);
		move(robot, xx2, yy2, xx3, yy3, 100, 100);
		sleep(robot, 1000);
		click(robot);
		sleep(robot, 1000);
		move(robot, xx3, yy3, 1027, 823, 500, 100);// 移出
		sleep(robot, 100);
		move(robot, 1027, 823, 1227, 823, 1000, 100);
		sleep(robot, 100);
		move(robot, 1227, 823, 1227, 56, 1000, 100);
		sleep(robot, 100);
		move(robot, 1227, 56, 295, 56, 1000, 100);
		sleep(robot, 100);
		click(robot);
		sleep(robot, 3000);
		robot.keyPress(KeyEvent.VK_F7);
		robot.keyRelease(KeyEvent.VK_F7);
		sleep(robot, 100);
	}

	private static void click(Robot robot) {
		robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
		robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
	}

	private static void dbclick(Robot robot) {
		robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
		robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
		robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
	}

	private static void move(Robot robot, int x, int y) {
		robot.mouseMove(x, y);
	}

	private static void sleep(Robot robot, int second) {
		robot.delay(second);
	}

	private static void sleep(int second) {
		try {
			Thread.sleep(second);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

	}

	private static void move(Robot robot, int x1, int y1, int x2, int y2, int second, int count) {
		long period = second / count;
		cc = 0;
		while (true) {
			int x = x1 + Math.round((x2 - x1) * cc / count);
			int y = y1 + Math.round((y2 - y1) * cc / count);
			if (cc <= count) {
				robot.mouseMove(x, y);
				cc++;
			} else {
				break;
			}
			robot.delay((int) period);
		}
	}

}
